// 我的
const baseUrl = 'https://gsdse.cn/api/KJRecruit';

Page({
  data: {
    personInfo: {
      avatarUrl:undefined,
      userId: 0,
      applyId: 0,
      userName: undefined,
      phoneNumber: '',
      email: '',
      positionName: 0,
      position: '',
      departmentId: 0,
      schoolClass: '',
      wxuserId: 0,
    },
    code: '',
    isLogin: false,
    isModalVisible: false,
    developers: [{
        role: '开发导师',
        names: ['吴颖浩']
      },
      {
        role: '项目负责人',
        names: ['黄伟聪']
      },
      {
        role: '项目开发',
        names: ['陈凯新', '方嘉妍', '劳俊诚', '冯佩婷']
      },
      {
        role: '插画设计',
        names: ['朱晓彤']
      },
      {
        role: 'banner设计',
        names: ['陈亮嘉']
      },
      {
        role: '手顺制作',
        names: ['李森', '刘彩烨', '董婉儿']
      },
      {
        role: '后端接口制作',
        names: ['冯皓理', '王桐']
      },
      {
        role: '前端接口优化',
        names: ['杨俊豪', '林夏栾']
      },
      {
        role: '前端功能优化、后端优化',
        names: ['许文铉']
      }
    ],
    screenHeight: '',
    screenWidth: '',
    isTouch: false,
    time: 0, //点击登录按钮的次数
  },

  // 复制
  copy() {
    const that = this
    wx.setClipboardData({
      data: that.data.personInfo.userId.toString(),
      success: function () {
        getApp().showSuccess("已复制");
      },
      fail: function (err) {
        getApp().showError("复制失败！")
        console.error(err)
      }
    })
  },

  //页面跳转
  //跳转到面试群二维码投放
  navTouploadbarcode() {
    wx.navigateTo({
      url: '/pages/uploadbarcode/uploadbarcode',
    })
    // getApp().barrier(false)
  },
  //跳转到查看报名表页面
  navToViewApplication() {
    wx.navigateTo({
      url: '/pages/viewApplication/viewApplication?departmentId=' + getApp().data.personInfo.departmentId,
    })
  },
  //跳转到编辑个人资料页面
  navToPersonnalInfomation(userId) {
    if (getApp().data._openid) {
      wx.navigateTo({
        url: '/pages/personalInformation/personalInformation?openId=' + getApp().data._openid,
      })
    } else {
      wx.navigateTo({
        url: '/pages/personalInformation/personalInformation?openId=' + this.data.personInfo._openid + '&userId=' + userId
      })
    }
  },
  navToMinisterialCertification() {
    wx.navigateTo({
      url: '/pages/MinisterialCertification/MinisterialCertification',
    })
  },
  navToAdministration() {
    wx.navigateTo({
      url: '/pages/Administration/Administration?departmentId=' + getApp().data.personInfo.departmentId,
    })
  },
  navToAdmissionInquiry() {
    wx.navigateTo({
      url: '/pages/admissionInquiry/admissionInquiry?openid=' + this.data.personInfo._openid,
    })
  },
  navTohot() {
    wx.navigateTo({
      url: '/pages/hot/hot',
    })
  },
  navToregistrationHistory() {
    wx.navigateTo({
      url: '../registrationHistory/registrationHistory',
    })
  },

  //致谢名单
  thankForus() {
    const that = this
    this.setData({
      isModalVisible: !that.data.isModalVisible
    });
  },

  //登录
  async login() {
    var that = this
    const globalMethod = getApp()
    //如果点击次数过多则提醒刷新页面
    that.setData({
      time: that.data.time + 1
    })
    console.log(that.data.time)
    if (that.data.time >= 3) {
      getApp().showMessage("如果点击多次仍没有反应，请尝试重新进入页面");
      return;
    }
    //判断是否已经点过登录
    if (that.data.isLogin) {
      console.log('已登录')
      wx.showToast({
        title: '已点击登录，请勿重复操作',
        icon: 'none'
      })
      return;
    } else {
      that.setData({
        isLogin: true
      })
    }
    //调用登录方法
    await globalMethod.onLaunch()
    //再次获取全局变量
    that.getGlobalData()
  },

  //获取全局变量
  async getGlobalData() {
    const that = this
    //获取用户信息
    const userInfo = await getApp().selectUser(getApp().data.personInfo.userId)
    if (userInfo) {
      that.setData({
        personInfo: userInfo,
        'personInfo.avatarUrl':getApp().data.personInfo.avatarUrl
      })
      console.log("用户信息", that.data.personInfo)
      //如果用户信息为空
      if (!that.data.personInfo.userId) {
        that.setData({
          personInfo: {
            avatarUrl:undefined,
            userId: 0,
            applyId: 0,
            userName: undefined,
            phoneNumber: '',
            email: '',
            positionName: 0,
            position: '',
            departmentId: 0,
            schoolClass: '',
            wxuserId: 0,
          }
        })
      }
    } 
    if (that.data.time == 1) {
      //三秒后再加载一次
      setTimeout(() => {
        that.getGlobalData()
        that.setData({
          time: 0
        })
      }, 2000)
    }

  },

  //监听页面显示
  async onShow() {
    var that = this
    //获取全局变量
    this.getGlobalData()
    //重置登录状态
    this.setData({
      isLogin: false,
      time: 0
    })
    await getApp().IsPersonalInfoEdit()
    console.log(getApp().data.infoHaveEdited)
  },

  //控制底下页面长度
  onLoad: function () {
    const systemInfo = wx.getSystemInfoSync();
    this.setData({
      screenHeight: systemInfo.windowHeight,
      screenWidth: systemInfo.windowWidth
    })
  },

  onShareAppMessage() {
    const promise = new Promise(resolve => {
      setTimeout(() => {
        resolve({
          title: '广师大科教，了解一下？',
          imageUrl:'../../image/shareLogo.png'
        })
      }, 1000)
    })
    return {
      title: '广师大科教，了解一下？',
      path: "pages/mine/mine",
      promise 
    }
  },

  onShareTimeline(){
    const promise = new Promise(resolve => {
      setTimeout(() => {
        resolve({
          title: '广师大科教，了解一下？',
          imageUrl:'../../image/shareLogo.png'
        })
      }, 1000)
    })
    return {
      title: '广师大科教，了解一下？',
      promise 
    }
  }
})